/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.itu.modernai;

import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
import cz.cuni.amis.pogamut.base.utils.math.DistanceUtils;
import cz.cuni.amis.pogamut.base3d.worldview.object.IViewable;
import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weapon;
import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weaponry;
import cz.cuni.amis.pogamut.ut2004.bot.IUT2004Bot;
import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotModuleController;
import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.AddInventory;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.GiveInventory;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.SendMessage;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BotKilled;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Item;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
import cz.cuni.amis.pogamut.ut2004.utils.UT2004BotRunner;
import cz.cuni.amis.utils.exception.PogamutException;
import java.awt.MultipleGradientPaint;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import sun.nio.cs.ext.GB18030;

/**
 *
 * @author Christoffer
 * An attempt to control an UT2004 bot using GA to evolve rules
 * 
 */
@AgentScoped
public class GABot extends UT2004BotModuleController {
    
    //Variables for stats
    private int kills;
    private int deaths;
    private Map<Weapon, Integer[]> shotsFired;
    private Map<Weapon, Integer> killsPerWeapon;
    private GeneManager geneManager;
    private Gene m_Gene;
    private ItemType m_LastUsedWeapon;
    
     @Override
    public void prepareBot(UT2004Bot bot) {
         shotsFired = new HashMap<Weapon, Integer[]>();
         geneManager = new GeneManager();
         m_Gene = geneManager.getNextGene();
       
        // By uncommenting following line, you can make the bot to do the file logging of all its components
        //bot.getLogger().addDefaultFileHandler(new File("EmptyBot.log"));
    }
    
     
     private void UpdateShotsFired(Weapon weapon)
     { 
         if(shotsFired.containsKey(weapon))
             shotsFired.get(weapon)[0] = shotsFired.get(weapon)[0] + 1;
         else
             shotsFired.put(weapon, new Integer[] {1,0});
         
        
     }
     
     private void UpdateWeaponKills(Weapon weapon)
     {
         if(shotsFired.containsKey(weapon))
             shotsFired.get(weapon)[1]++;
         else
             throw new NullPointerException("One can not kill an enemy without using a weapon!");
     }
     /**
      * Logic which rule to use.
      * 
      * */
   @Override
    public void logic() throws PogamutException {
       
       
      if(players.canSeePlayers())
      {
          body.getCommunication().sendGlobalTextMessage("I SEE YOU!!");
      }

     
      //CollectNearestObject();
    }
   
   private void AddWeaponAndAmmo(ItemType weaponType, ItemType ammoType, int ammoPacks)
   {
       AddInventory weapon = new AddInventory();
       AddInventory ammo = new AddInventory();
       
       weapon.setType(weaponType.getName());
       ammo.setType(ammoType.getName());
       
       getAct().act(weapon);       
       for (int i = 0; i < ammoPacks; i++) {
           getAct().act(ammo);
       }
   }
    
    public void ChooseWeapon()
    {
        if(!players.canSeeEnemies())//no need to choose weapon, we cant see anyone
            return;
        Player enemy = players.getNearestVisiblePlayer(players.getVisibleEnemies().values());
        double distanceToEnemy = info.getLocation().getDistance(enemy.getLocation());
        
        //normalize to 0 - 1
        
        for (int i = 0; i < Gene.CHROMOSOME_SIZE; i++) 
        {
            if(m_Gene.getChromosomeElement(i) > distanceToEnemy && m_Gene.getChromosomeElement(i+1) < distanceToEnemy)
                EquipWeapon(i);
        }
    }

 
    public static void main (String args[]) throws PogamutException
    {
        new UT2004BotRunner(GABot.class, "GABoto").setMain(true).startAgent();
    }
    
    private void CollectNearestObject()
    {
        for (Iterator<Item> it = items.getSpawnedItems().values().iterator(); it.hasNext();) 
        {
            Item currItem = it.next();
            if(currItem.isVisible() && items.getAllItems(ItemType.Group.HEALTH).containsKey(currItem.getId()));
            {
                body.getCommunication().sendGlobalTextMessage("I SEE: " + currItem.getType().toString());
            }
        }        
    }

    private ItemType EquipWeapon(int i) {
        switch(i)
        {
            case 0:
                return ItemType.SHIELD_GUN;
            case 1:
                return ItemType.ASSAULT_RIFLE;
            case 2:
                 return ItemType.BIO_RIFLE;
            case 3:
                 return ItemType.SHOCK_RIFLE;
            case 4:
                return ItemType.LINK_GUN;
            case 5: 
                return ItemType.MINIGUN;
            case 6:
                return ItemType.FLAK_CANNON;
            case 7:
                return ItemType.LINK_GUN;
            case 8:
                return ItemType.LIGHTNING_GUN;
            case 9:
                return ItemType.REDEEMER;
            default:
                return weaponry.getCurrentWeapon().getType();
                
        }
    }
}
